/**
 * contains a hashmap of key[upc strings] to value[products]
 * @author yuji kuzuhara
 */

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.IOException;


public class ProductCatalog {

    private HashMap<String,Product> products;

    public ProductCatalog() {
      products = new HashMap<String,Product>(); 
      try{
      ProductReader productreader = new ProductReader("Products.txt");
      Product nextproduct = null;
        while(productreader.hasMoreProducts()){
          nextproduct = productreader.getNextProduct();
          products.put(nextproduct.getUPC(),nextproduct);
        }
      }catch(IOException e){
        System.out.println("error:"+e);
      }    
    }

    public Product getProductByUPC(String UPC) {
        return products.get(UPC);
    }

    public void addProduct(Product product) {
        products.put(product.getUPC(),product);
    }

    public ArrayList<Product> getProductList() {
        ArrayList<Product> productlist=new ArrayList<Product>();
        Iterator iter = products.entrySet().iterator();
        while (iter.hasNext()) {
            productlist.add((Product)iter.next());
        }
        return productlist;
    }

}

